package com.shababeach.travel.service.impl;



import com.shababeach.travel.entity.Location;
import com.shababeach.travel.entity.LocationType;
import com.shababeach.travel.repository.LocationRepository;
import com.shababeach.travel.service.LocationService;
import com.shababeach.travel.service.LocationTypeService;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 位置信息Service实现类
 */
@Service
public class LocationServiceImpl implements LocationService {

    @Autowired
    private LocationTypeService locationTypeService;
    @Autowired
    private LocationRepository locationRepository;

    @Override
    public Page<Location> pageList(Pageable pageable, Integer typeId, String keyword) {
        Specification<Location> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            // 按类型ID查询
            if (typeId != null && typeId > 0) {
                predicates.add(cb.equal(root.get("typeId"), typeId));
            }

            // 按名称或地址模糊查询
            if (StringUtils.hasText(keyword)) {
                Predicate nameLike = cb.like(root.get("name"), "%" + keyword + "%");
                Predicate addressLike = cb.like(root.get("address"), "%" + keyword + "%");
                predicates.add(cb.or(nameLike, addressLike));
            }

            return cb.and(predicates.toArray(new Predicate[0]));
        };

        // 执行分页查询
        Page<Location> pageResult = locationRepository.findAll(spec, pageable);

        // 补充位置类型信息
        pageResult.forEach(this::fillLocationType);

        return pageResult;
    }


    /**
     * 获取推荐位置列表
     */
    @Override
    public List<Location> getRecommendLocations() {
        List<Location> locationList = locationRepository.findByStatusAndIsRecommend(1, 1);
        locationList.forEach(this::fillLocationType);
        return locationList;
    }

    /** 获取指定类型的位置列表 */
    @Override
    public List<Location> getLocationsByType(Integer typeId) {
        List<Location> locationList = locationRepository.findByTypeId(typeId);
        locationList.forEach(this::fillLocationType);
        return locationList;
    }

    /** 获取位置详情 */
    @Override
    public Location getLocationDetail(Integer id) {
        Optional<Location> location = locationRepository.findById(id);
        if (location.isPresent()) {
            fillLocationType(location.orElse(null));
        }
        return location.orElse(null);
    }

    /**
     * 增加位置浏览次数
     */
    @Override
    public void increaseViewCount(Integer id) {
        Location location = locationRepository.findById(id).orElse(null);
        if (location != null) {
            location.setViewCount(location.getViewCount() + 1);
            locationRepository.save(location);
        }
    }

    /** 根据ID获取 Location */
    @Override
    public Location getById(Integer id) {
        return locationRepository.findById(id).orElse(null);
    }

    /**
     * 填充位置类型信息
     */
    private void fillLocationType(Location location) {
        if (location != null && location.getTypeId() != null) {
            LocationType locationType = locationTypeService.getById(location.getTypeId());
            location.setLocationType(locationType);
        }
    }
} 